package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lkd.domain.Task;
import com.lkd.domain.TimeInterval;
import com.lkd.mapper.TaskMapper;
import com.lkd.service.ITaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.vo.TaskVo;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 工单表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-12
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

    @Override
    public List<TaskVo> coungetTaskReportInfo(TimeInterval timeInterval) {
        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>();
        wrapper.le(Task::getCreateTime,timeInterval.getEnd()).ge(Task::getCreateTime,timeInterval.getStart());
        List<Task> tasks = list(wrapper);
        //根据是否是维修工单分类
        Map<Integer, List<Task>> map = tasks.stream().collect(Collectors.groupingBy(Task::getProductTypeId));
        List<TaskVo> list = new ArrayList<>();
        map.forEach(new BiConsumer<Integer, List<Task>>() {
            @Override
            public void accept(Integer type, List<Task> tasks) {
                int completedTotal = 0,cancelTotal = 0,progressTotal = 0,workerCount = 0;
                //type 1.表示维修工单2是业务工单
                Boolean repair = type.equals(1)?true:false;


                for (Task task : tasks) {
                    if (task.getTaskStatus().equals(1)){
                        workerCount++;
                    }else if (task.getTaskStatus().equals(2)){
                        progressTotal++;
                    }else if (task.getTaskStatus().equals(3)){
                        cancelTotal++;
                    }else {
                        cancelTotal++;
                    }
                }
                TaskVo taskVo = TaskVo.builder().total(tasks.size())
                        .date(timeInterval.toString())
                        .repair(repair)
                        .completedTotal(completedTotal)
                        .cancelTotal(cancelTotal)
                        .progressTotal(progressTotal)
                        .workerCount(workerCount)
                        .build();
               list.add(taskVo);
            }
        });
        return list;
    }
}
